/**
 * @Title: HomeAdapter.java
 * @Package: com.jason.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日 上午9:21:22
 * @Version: V1.0
 */
package com.jason.fingerprint.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.jason.fingerprint.R;
import com.jason.fingerprint.ui.PhotoManagerActivity;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PhotoManagerAdapter
 * @Description: 图片管理 GridView 适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-12-4 09:24:43
 */
public class PhotoManagerAdapter extends BaseAdapter {

	private static final String TAG = PhotoManagerAdapter.class.getSimpleName();
	public static final String CAMERA = "Camera";

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<String> mDatas = new ArrayList<String>();
	private boolean mIsManager = false;
	private boolean mIsShowCamera;

	public PhotoManagerAdapter(Context context){
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mDatas.add(CAMERA);
	}

	public PhotoManagerAdapter(Context context,boolean isShowCamera){
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mIsShowCamera = isShowCamera;
		if (mIsShowCamera){
			mDatas.add(CAMERA);
		}
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addAll(List<String> datas,boolean isManager) {
		this.mDatas = datas;
		if (mDatas == null){
			mDatas = new ArrayList<String>();
			mDatas.add(CAMERA);
		}
		this.mIsManager = isManager;
		notifyDataSetChanged();
	}

	public void addAll(List<String> datas,boolean isManager,boolean isShowCamera) {
		this.mDatas = datas;
		if (mDatas == null){
			mDatas = new ArrayList<String>();
			if (isShowCamera){
				mDatas.add(CAMERA);
			}
		}
		this.mIsManager = isManager;
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.adapter_photo_manager, null);
			holder = new Holder();
			holder.mPhotoView = (ImageView) convertView.findViewById(R.id.item_photo);
			holder.mDeleteView = (ImageView) convertView.findViewById(R.id.item_delete);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		final String path = (String)getItem(position);
		Log.i(TAG,">>>>>>>> position: " + position + ",path:" + path);
		holder.mDeleteView.setVisibility(View.GONE);
		if (!TextUtils.isEmpty(path)) {
			if (CAMERA.equals(path)){
				holder.mPhotoView.setBackgroundResource(R.drawable.ic_camera);
				holder.mPhotoView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						//TODO 打开照相机
						((PhotoManagerActivity)mContext).doTakePhoto();
					}
				});
			}else{
				holder.mPhotoView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						//TODO 进入图片浏览页面
//						ArrayList<String> paths = new ArrayList<String>();
//						paths.add(path);
//						Intent mIntent = new Intent(mContext, PreviewPhotoActivity.class);
//						mIntent.putStringArrayListExtra("images", paths);
//						mIntent.putExtra("position", 0);
//						mContext.startActivity(mIntent);
						((PhotoManagerActivity)mContext).lookPhoto(position);
					}
				});
				if (mIsManager){
					holder.mDeleteView.setVisibility(View.VISIBLE);
					holder.mDeleteView.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							//TODO 删除图片，然后更新整个页面
							((PhotoManagerActivity)mContext).deletePhoto(path);
						}
					});
				}
				//TODO 显示图片
				FileInputStream f = null;
				try {
					f = new FileInputStream(path);
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inSampleSize = 8;//图片的长宽都是原来的1/8
					BufferedInputStream bis = new BufferedInputStream(f);
					Bitmap bm = BitmapFactory.decodeStream(bis, null, options);
					holder.mPhotoView.setImageBitmap(bm);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}

				//ImageLoader.getInstance().displayImage("file://" + path,holder.mPhotoView,mDisplayImageOptions);
			}
		}
		return convertView;
	}
	
	class Holder{
		ImageView mPhotoView;
		ImageView mDeleteView;
	}
	
}
